import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
// pages/catepory/index.js
Page( {
  data: {
  leftMenuList:[],
  rightContent:[],
  currentIndex:0,
  ScrollTop:0
  },
  Cates:[],

  onLoad: function (options) {
    //缓存设置判断是否有旧数据可以提取    
    const Cates = wx.getStorageSync("cates");
    if(!Cates){
      //不存在发送数据请求
      this.getCates();
    }else{
      //有旧数据
      if(Date.now()-Cates.time>1000*10){
        this.getCates();
      }else{
        this.Cates=Cates.data;
        let leftMenuList=this.Cates.map(v=>v.classify_tag);
        let rightContent=this.Cates[0].children;
        this.setData({
          leftMenuList,
          rightContent
        })
      }

    }
  },
  //获取分类数据
  async getCates(){
    const res=await request({url:"/categories"});
    this.Cates=res;
        //存储数据到本地
        wx.setStorageSync("cates",{time:Date.now(),data:this.Cates});
        //构造左侧的大菜单数据
        let leftMenuList=this.Cates.map(v=>v.classify_tag);
        let rightContent=this.Cates[0].children;
        //构造右侧的商品时间
        this.setData({
          leftMenuList,
          rightContent
        })
  } ,
  //左侧菜单的点击事件
  handleItemTap(e){
    const {index}=e.currentTarget.dataset;
    let rightContent=this.Cates[index].children;
    this.setData({
      currentIndex:index,
      rightContent,
      ScrollTop:0
    })
  }
})